#pragma once

template<size_t grow_size>
class mem_pool3  
{
	struct big_buf
	{
		big_buf*m_next;
		char m_mem[8];
	};
private:
	big_buf* m_big_buf;
	char*    m_buf;
	size_t   m_pos;
public:
	mem_pool3()
	{
		m_big_buf=NULL;
		m_buf=NULL;
		m_pos=0;
	}
	void*mem_malloc(size_t size)
	{
		if(m_buf==NULL || m_pos+size>grow_size)new_buf();
		char*buf=&m_buf[m_pos];
		m_pos+=size;
		return buf;
	}
	void mem_free(void*p)
	{
	}
	~mem_pool3()
	{
		big_buf*buf=m_big_buf;
		while(buf)
		{
			big_buf*tmp=buf;
			buf=buf->m_next;
			free(tmp);
		}
	}
	void mem_reset()
	{
		big_buf*buf=m_big_buf;
		size_t i=0;
		while(buf)
		{
			big_buf*tmp=buf;
			buf=buf->m_next;
			if(i>0)free(tmp);
			++i;
		}
		if(m_big_buf)
		{
			m_big_buf->m_next=0;
			m_buf=m_big_buf->m_mem;
		}
		m_pos=0;
	}
private:
	void new_buf()
	{
		big_buf*buf=(big_buf*)malloc(sizeof(big_buf)+grow_size-8);
		if(!buf)throw;
		m_buf=buf->m_mem;
		buf->m_next=m_big_buf;
		m_big_buf=buf;
		m_pos=0;
	}
};
